#!/bin/ash -efu

git_config_foreach_test01() { # UnitTest
	. ../shell-git-config
	local result expect key="aaa.bar"

	result="$(
		git_config_foreach_handler()
		{
			printf '%s\n' "$2"
		}
		git_config_env git-config/config0.ini;
		git_config_foreach "$key" git_config_foreach_handler;
	)"
	expect="6
7"
	assertEquals "List values failed (key '$key'): $result != $expect" "$result" "$expect"
}

git_config_foreach_test02() { # UnitTest
	. ../shell-git-config
	local result expect key="aaa.bbb.ccc"

	result="$(
		git_config_foreach_handler()
		{
			printf '%s\n' "$2"
		}
		git_config_env git-config/config1.ini;
		git_config_foreach "$key" git_config_foreach_handler;
	)"
	expect="1
3
5"
	assertEquals "List values failed (key '$key'): $result != $expect" "$result" "$expect"
}

git_config_foreach_test03() { # UnitTest
	. ../shell-git-config
	local result expect key="missing.section.key"

	result="$(git_config_env git-config/config1.ini; git_config_foreach "$key")"
	expect=""

	assertEquals "List values failed (key '$key'): $result != $expect" "$result" "$expect"
}

git_config_foreach_test04() { # UnitTest
	. ../shell-git-config
	local result expect

	result="$(git_config_env git-config/config0.ini; git_config_foreach)"
	expect="bbb.foo=1
aaa.bar=6
aaa.bar=7
ccc.bar.foo=1
ddd.bar.foo=1
ddd.bar.baz=2
ddd.bar.foo=3
ddd.bar.baz=4
eee.fff.xxx=5
fff.bar=xxx
zzz
fff.baz=xxx	zzz
fff.foo=xxxzzz
fff.quote= xxx  zzz
ggg.foo=aaa    bbc
quote.foo0= zzz "

	assertEquals "List all values" "$result" "$expect"
}
